package org.playground.booking.utils;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Node;

import java.lang.reflect.Field;

public class CommonUtils {

    public static final Integer PAGING_MAX_LIMIT=50;


    public static java.text.SimpleDateFormat getDateFormat(){
        java.text.SimpleDateFormat simple =new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return simple;
    }

    public static String objectToXml(Object object) throws IllegalAccessException {
        Class classOfObj = object.getClass();

        Field[] fields = classOfObj.getDeclaredFields();
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for(Field field : fields){
            String name = field.getName();
            Object value = field.get(object);
            sb.append("<"+name+">");
            sb.append(String.valueOf(value));
            sb.append("</"+name+">");
        }
        sb.append("</xml>");
        return sb.toString();
    }

    public static String getCDataValue(Node node){
        Node first = node.getFirstChild();
        if(first instanceof CharacterData){
            CharacterData cd = (CharacterData)first;
            return cd.getData();
        }else{
            return "";
        }
    }
}
